package NetWork;

import java.net.*;
import java.util.Scanner;

public class Client1 {
    public static void main(String[] args) throws Exception {

        //创建一个客户端
        DatagramSocket datagramSocket = new DatagramSocket();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输出:");
            String s = scanner.nextLine();
            if (s.equals("exit")){
                datagramSocket.close();
                System.out.println("成功退出程序!!!!");
                break;
            }
            byte[] bytes = s.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length, InetAddress.getLocalHost(),9090);
            datagramSocket.send(packet);

            //接收服务器响应的数据
            byte[] bytes1 = new byte[1024 * 8];
            DatagramPacket packet1 = new DatagramPacket(bytes1,bytes1.length);
            datagramSocket.receive(packet1);

            //将响应回来的数据进行输出
            System.out.println(new String(bytes1,0,packet1.getLength()));
            System.out.println("来自服务器IP为:" + packet1.getAddress().getHostAddress() + " ，来自服务器的端口号为:" + packet1.getPort());
            System.out.println("====================================================");
        }


/*        //关闭资源
        datagramSocket.close();*/

    }
}
